import app from './main'
const axios = require('axios')
const http = axios.create({
  // baseURL: 'http://localhost:5000/web/api',
  baseURL: process.env.VUE_APP_API_URL || '/web/api',
  timeout: 5000
})

http.interceptors.response.use(
  res => {
    return res
  },
  err => {
    console.log(err)
    const errMessage = err.response.data.message
    console.log(errMessage)
    if (errMessage) {
      // 使用 Vue 的 $message
      app.config.globalProperties.$message.error(errMessage)
    }
    return Promise.reject(err)
  }
)

http.interceptors.request.use(
  function(config) {
    if (localStorage.token) {
      config.headers.Authorization = 'Bearer ' + localStorage.token
    }
    return config
  },
  function(error) {
    return Promise.reject(error)
  }
)

export default http
